package com.yoga.service.impl;

import com.yoga.entity.YogaCoach;
import com.yoga.entity.YogaMyorder;
import com.yoga.entity.YogaPrivaterequest;
import com.yoga.entity.YogaPrivaterequestExample;
import com.yoga.mapper.YogaCoachDao;
import com.yoga.mapper.YogaMyorderDao;
import com.yoga.mapper.YogaPrivaterequestDao;
import com.yoga.service.YogaUserService;
import com.yoga.service.CoachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CoachServiceImpl implements CoachService {
    @Autowired
    private YogaCoachDao yogaCoachDao;
    @Autowired
    private YogaMyorderDao yogaMyorderDao;
    @Autowired
    private YogaPrivaterequestDao yogaPrivaterequestDao;
    @Autowired
    private YogaUserService yogaUserService;

    @Override
    public List<YogaPrivaterequest> findMyClass(Integer coachuid) {
        YogaPrivaterequestExample example = new YogaPrivaterequestExample();
        example.createCriteria().andCoachuidEqualTo(String.valueOf(coachuid));
        List<YogaPrivaterequest> list = yogaPrivaterequestDao.selectByExample(example);
        return list;
    }

    @Override
    public List<YogaCoach> getAllCoach() {
        return yogaCoachDao.selectByExample(null);
    }

    @Override
    public YogaCoach getCoachById(Integer uid) {
        return yogaCoachDao.selectByPrimaryKey(uid);
    }

    @Override
    public boolean addCoach(YogaCoach coach) {
        return yogaCoachDao.insertSelective(coach) > 0 ? true : false;
    }

    @Override
    public boolean updateCoach(YogaCoach coach) {
        return yogaCoachDao.updateByPrimaryKeySelective(coach) > 0 ? true : false;
    }

    @Override
    public boolean deleteCoach(Integer uid) {

        return yogaCoachDao.deleteByPrimaryKey(uid) > 0 ? true : false;

    }

    @Override
    public boolean changeClassState2Ready(Integer pqid, Integer stateCode) {
        YogaPrivaterequest lesson = yogaPrivaterequestDao.selectByPrimaryKey(pqid);
        if(stateCode == 1){
            lesson.setPstatus("已确认");
        } else if (stateCode == 2) {
            lesson.setPstatus("已取消");
        }else{
            lesson.setPstatus("已支付");
        }
        Integer uid = Integer.valueOf(lesson.getUid());
        Integer classmoney = lesson.getClassmoney();
        Integer pqid1 = lesson.getPqid();
        if(lesson.getPstatus().equals("已确认")||lesson.getPstatus().equals("已取消")){
            YogaMyorder yogaMyorder = new YogaMyorder(pqid1, null,
                    null, null, null, null, lesson.getPstatus());
            yogaMyorderDao.updateByPrimaryKeySelective(yogaMyorder);
            yogaUserService.updataMoney(pqid1, BigDecimal.valueOf(classmoney),uid,lesson.getPstatus());
            yogaPrivaterequestDao.updateByPrimaryKeySelective(lesson);
        }else {
            yogaUserService.updataMoney(pqid1, BigDecimal.valueOf(classmoney),uid,"已支付");
            yogaPrivaterequestDao.updateByPrimaryKeySelective(lesson);
        }
        return true;
    }

//    @Override
//    public boolean changeClassState2NoReady(Integer pqid) {
//        YogaPrivaterequest lesson = yogaPrivaterequestDao.selectByPrimaryKey(pqid);
//        if (lesson.getPstatus().equals("教练未确认") || lesson.getPstatus().equals("教练已确认")) {
//            lesson.setPstatus("已取消");
//        }
//        return true;
//    }

    @Override
    public Boolean newLessonRequest(Integer uid, Integer coachuid, Integer price, Integer cpid, List<String> requesttime) {
        YogaPrivaterequest yogaPrivaterequest = new YogaPrivaterequest();
        int size = requesttime.size();
        price = price * size;
        String time = requesttime.toString().substring(1, requesttime.toString().length() - 1);
        yogaPrivaterequest.setUid(String.valueOf(uid));
        yogaPrivaterequest.setCoachuid(String.valueOf(coachuid));
        yogaPrivaterequest.setPstatus("教练未确认");
        yogaPrivaterequest.setRequesttime(new Date());
        yogaPrivaterequest.setClassmoney(price);
        yogaPrivaterequest.setCpid(cpid);
        yogaPrivaterequest.setVid(null);
        yogaPrivaterequest.setPrivatetime(time);
        int i = yogaPrivaterequestDao.insert(yogaPrivaterequest);
        YogaMyorder yogaMyorder = new YogaMyorder(null, String.valueOf(uid), String.valueOf(coachuid),
                price, new Date(), null, "待确认");
        yogaMyorderDao.insert(yogaMyorder);
        return i > 0 ? true : false;
    }
}
